package dk.sdu.flexibus;

import java.util.Calendar;
import java.util.List;

import dk.sdu.flexibus.communications.GetPickupInformationListener;
import dk.sdu.flexibus.communications.GetPickupInformationProvider;
import dk.sdu.flexibus.domain.Transport;
import dk.sdu.flexibus.persistence.Persister;
import dk.sdu.flexibus.persistence.TransportFactory;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);

		return true;
	}

	public void indicateAddress(View view) {
		Intent intent = new Intent(this, IndicateAddressActivity.class);
		startActivity(intent);
	}
	
	public void clearStore(View view) {
		Persister.clearStore(getPreferences());
	}
	
	private SharedPreferences getPreferences() {
		return getSharedPreferences(Persister.PREFS_FILE_NAME,
				MODE_PRIVATE);
	}
	
	public void checkTransports(View view) {
		Intent intent = new Intent(this, ShowAllTransportsActivity.class);
		startActivity(intent);
	}
	
	public void setupProgram(View view) {
		checkForUpdates(getPreferences());
	}	
	
	private void checkForUpdates(SharedPreferences preferences){
		List<Integer> transportIds = TransportFactory.findTransportIdsByState(getPreferences(), new int[]{Transport.STATE_SENT});
		for(int id : transportIds){
			Transport t = TransportFactory.readTransport(getPreferences(), id);
			GetPickupInformationProvider provider = new GetPickupInformationProvider(t.getServerId(), new UpdatingGetPickupInformationListener(preferences));
			provider.sendRequest();
		}
		
		transportIds = TransportFactory.findTransportIdsByState(getPreferences(), new int[]{Transport.STATE_OFFERED});
		Calendar now = Calendar.getInstance();
		for(int id : transportIds){
			Transport t = TransportFactory.readTransport(getPreferences(), id);
			if(t.getDropOffTime().after(now)){
				t.setTransportState(Transport.STATE_COMPLETED);
				TransportFactory.writeTransport(getPreferences(), t);
			}
			GetPickupInformationProvider provider = new GetPickupInformationProvider(t.getServerId(), new UpdatingGetPickupInformationListener(preferences));
			provider.sendRequest();
		}
	}

	private class UpdatingGetPickupInformationListener  implements GetPickupInformationListener{
		SharedPreferences preferences;
		public UpdatingGetPickupInformationListener(
				SharedPreferences preferences) {
			this.preferences = preferences;
		}

		@Override
		public void sendPickupInformation(String errorCode,
				Calendar pickUpTime, int transportID) {
			if(errorCode.equals("NotCalculatedYet")) return;
			if(errorCode.equals("NotFound")) Log.d(this.getClass().toString(), "Transport not found on server: "+transportID);
			int id = TransportFactory.findTransportIdsByServerId(preferences, transportID);
			
			Transport t = TransportFactory.readTransport(preferences, id);
			if(errorCode.equals("Rejected")){
				t.setTransportState(Transport.STATE_REJECTED);
				TransportFactory.writeTransport(preferences, t);
			} else if(errorCode.equals("None")){
				t.setTransportState(Transport.STATE_OFFERED);
				t.setPickupTime(pickUpTime);
				TransportFactory.writeTransport(preferences, t);
			} else {
				Log.d(this.getClass().toString(), "Response did not include an errorCode. TransportId: "+transportID);
				return;
			}	
		}

		@Override
		public void sendError(Exception exception) {
			// TODO Auto-generated method stub	
		}
	}
}
